package com.gxl.butt.DAO.impl;

import com.gxl.butt.DAO.IHardUserDAO;
import com.gxl.butt.domain.HardUser;
import org.apache.commons.collections.map.HashedMap;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by GUO XIAOLIN on 2017/5/8.
 */
@Repository
public class HardUserDAOImpl extends HibernateBaseDao<HardUser> implements IHardUserDAO {

    @Override
    public Integer insertUser(String account, String password, String nickname,String idcard) {
        HardUser hardUser  = new HardUser();
        hardUser.setAccount(account);
        hardUser.setNickname(nickname);
        hardUser.setPassowrd(password);
        hardUser.setIdcard(idcard);

        this.save(hardUser);

        return 1;
    }

    @Override
    public Long insertUser(HardUser user) {
        return (Long) this.save(user);
    }

    @Override
    public Long selectUserByOpenid(String openid) {
        String hql = "from HardUser harduser where harduser.openid = '"+ openid + "' ";
        List<HardUser> list = this.getListByHql(hql);
        if (list != null && list.size() > 0) {
            return list.get(0).getId();
        }
        return  0L;
    }

    @Override
    public List<HardUser> selectUserByAccountAndPassword(String account, String password) {

        String hql = "from HardUser harduser where harduser.account = '" + account + "' and harduser.passowrd = '" + password + "' ";
//        List<HardUser> list = (List<HardUser>) getHibernateTemplate().find(hql, account,password);
        List<HardUser> list = this.getListByHql(hql);
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public Integer selectUserByAccount(String account) {
        String hql = "from HardUser harduser where harduser.account = '" + account + "'";
//        List<HardUser> list = (List<HardUser>) getHibernateTemplate().find(hql, account);
        List<HardUser> list = this.getListByHql(hql);
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public List<HardUser> selectUserByIdCard(String idcard) {
        String hql = "from HardUser harduser where harduser.idcard = '" + idcard +"'";
//        List<HardUser> list = (List<HardUser>) getHibernateTemplate().find(hql,idcard);
        List<HardUser> list = this.getListByHql(hql);
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public List<HardUser> getTenUser() {
        final String hql = "from HardUser harduser order by harduser.id desc";
        Map<String, Object> map = new HashMap<String, Object>();
        return this.getListByHql(hql, 1, 10, map);
    }
}
